{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "456ba1aa",
   "metadata": {},
   "source": [
    "## 字符串格式化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0c9f1ab0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-13T09:10:15.120153Z",
     "start_time": "2023-10-13T09:10:15.114829Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名：张三,年龄：20\n",
      "姓名：张三,年龄：20\n",
      "姓名：张三,年龄：20\n",
      "名称1：Java, 名称2：Python\n",
      "a加b运算：30\n",
      "表达式运算的结果：90\n"
     ]
    }
   ],
   "source": [
    "# 旧版本\n",
    "name = '张三'\n",
    "age = 20\n",
    "print('姓名：%s,年龄：%d' % (name, age))\n",
    "\n",
    "# 使用format\n",
    "print('姓名：{},年龄：{}'.format(name, age))\n",
    "\n",
    "# 新版本\n",
    "print(f'姓名：{name},年龄：{age}')\n",
    "\n",
    "names = ['Java', 'Python', '前端']\n",
    "print(f'名称1：{names[0]}, 名称2：{names[1]}')\n",
    "\n",
    "# 表达式\n",
    "a = 10\n",
    "b = 20\n",
    "print(f'a加b运算：{a+b}')\n",
    "print(f'表达式运算的结果：{3*(a+b)}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2114b1e9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-13T09:11:28.654742Z",
     "start_time": "2023-10-13T09:11:28.651836Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a=10, b=20\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "b = 20\n",
    "print(f'{a=}, {b=}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "52945ed6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-13T09:12:16.959306Z",
     "start_time": "2023-10-13T09:12:16.955112Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "******baizhan*******\n",
      "******baizhan*******\n",
      "*************baizhan\n",
      "baizhan*************\n"
     ]
    }
   ],
   "source": [
    "# 使用指定的字符填充\n",
    "name = 'baizhan'\n",
    "\n",
    "# 长度为20  使用*填充  居中显示\n",
    "print('{:*^20}'.format(name))\n",
    "print(f'{name:*^20}')\n",
    "\n",
    "# 居右显示\n",
    "print(f'{name:*>20}')\n",
    "\n",
    "# 居左显示\n",
    "print(f'{name:*<20}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d6d3f917",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-13T09:12:32.980540Z",
     "start_time": "2023-10-13T09:12:32.976417Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12.35\n",
      "12.35\n",
      "num=12.0\n",
      "78.90%\n",
      "79%\n"
     ]
    }
   ],
   "source": [
    "# 对数值变量的格式化\n",
    "price = 12.345\n",
    "print('{:.2f}'.format(price))\n",
    "print(f'{price:.2f}')\n",
    "\n",
    "num = 12\n",
    "print(f'{num=:.1f}')\n",
    "\n",
    "pct = 0.789\n",
    "print('{:.2f}%'.format(pct*100))\n",
    "print(f'{pct*100:.0f}%')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79a79d21",
   "metadata": {},
   "source": [
    "## 字符串新增方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22478730",
   "metadata": {},
   "source": [
    "![](images/20.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7b6ff460",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-13T09:14:11.108792Z",
     "start_time": "2023-10-13T09:14:11.104645Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "world\n",
      "helloworld\n",
      "hellowor\n",
      "helloworld\n"
     ]
    }
   ],
   "source": [
    "# 删除前缀\n",
    "result = 'helloworld'\n",
    "print(result.removeprefix('hello'))\n",
    "print(result.removeprefix('abc'))\n",
    "\n",
    "# 删除后缀\n",
    "print(result.removesuffix('ld'))\n",
    "print(result.removesuffix('World'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fbca3a6a",
   "metadata": {},
   "source": [
    "## 变量类型标注\n",
    "变量类型注解是用来对变量和函数的参数返回值类型做注解（暗示），帮助开发者写出更加严谨的代码，让调用方减少类型方面的错误，也可以提高代码的可读性和易用性。\n",
    "\n",
    "但是，变量类型注解语法传入的类型表述能力有限，不能说明复杂的类型组成情况，因此引入了 typing 模块，来实现复杂的类型表达。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "473f3895",
   "metadata": {},
   "source": [
    "**常用的数据类型：**\n",
    "\n",
    "![](images/21.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97c9de91",
   "metadata": {},
   "source": [
    "![](images/22.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c17afeeb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-13T09:19:20.075147Z",
     "start_time": "2023-10-13T09:19:20.063256Z"
    }
   },
   "outputs": [],
   "source": [
    "# 简单类型变量标注\n",
    "from typing import List, Set, Dict, Tuple\n",
    "a: int = 10\n",
    "b: str = 'hello'\n",
    "c: float = 3.14\n",
    "d: bool = False\n",
    "e: bytes = b'world'\n",
    "\n",
    "# 复杂类型\n",
    "x: List[int] = [1, 3, 4, 5]\n",
    "x: Set[str] = {'ab', 'cd', 'ef'}\n",
    "x: Dict[str, str] = {'name': 'zs', 'sex': 'male', 'age': 23}\n",
    "x: Dict[str, object] = {'name': 'zs', 'sex': 'male', 'age': 23}\n",
    "x: Tuple[int] = (3,)\n",
    "x: Tuple[int, int, int] = (3, 2, 4)\n",
    "x: Tuple[int, str, float] = (10, 'hello', 23.4)\n",
    "\n",
    "# 定义可变大小的元组，使用省略号\n",
    "x: Tuple[int, ...] = (1, 2, 3, 4, 5, 9, 6)\n",
    "\n",
    "# 3.10新特性 list tuple  dict  set\n",
    "x: list[str] = ['a', 'b', 'c']\n",
    "x: tuple[int] = (2,)\n",
    "x: tuple[int, int] = (2, 3)\n",
    "x: tuple[int, ...] = (1, 2, 3, 4, 5, 5)\n",
    "x: set[str] = {'aa', 'bb', 'cc'}\n",
    "x: dict[str, object] = {'k1': 1, 'k2': 'aa', 'k3': True}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95c5e988",
   "metadata": {},
   "source": [
    "## 函数参数与返回值 类型标注"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "15a3e5e5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-13T09:22:31.234561Z",
     "start_time": "2023-10-13T09:22:31.230667Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\n"
     ]
    }
   ],
   "source": [
    "from typing import Iterator\n",
    "from typing import Callable\n",
    "\n",
    "\n",
    "def num_fun(num: int) -> str:\n",
    "    \"\"\"\n",
    "    参数 num int类型\n",
    "    返回值 字符串类型\n",
    "    \"\"\"\n",
    "    return str(num)\n",
    "\n",
    "\n",
    "print(num_fun('100'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f2eeb770",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-13T09:23:55.859460Z",
     "start_time": "2023-10-13T09:23:55.855264Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "30\n",
      "22.34\n"
     ]
    }
   ],
   "source": [
    "def sum_fun(a: int, b: int) -> int:\n",
    "    \"\"\"定义函数 两个参数都是int 返回int\"\"\"\n",
    "    return a+b\n",
    "\n",
    "\n",
    "def fun_test(num1: int, num2: float = 12.34) -> float:\n",
    "    \"\"\"定义函数参数添加默认值\"\"\"\n",
    "    return num1+num2\n",
    "\n",
    "print(fun_test(10, 20))\n",
    "print(fun_test(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "5b1115c6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-13T09:24:50.748311Z",
     "start_time": "2023-10-13T09:24:50.744795Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "300\n"
     ]
    }
   ],
   "source": [
    "# 定义变量 指向一个函数\n",
    "f: Callable[[int, int], int] = sum_fun\n",
    "    \n",
    "print(f(100, 200))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "71f3ca80",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-13T09:25:13.620637Z",
     "start_time": "2023-10-13T09:25:13.616275Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<generator object return_fun at 0x1209b2a50>\n",
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "def return_fun(num: int) -> Iterator[int]:\n",
    "    \"\"\"定义函数，产生整数的生成器，每次返回一个\"\"\"\n",
    "    i = 0\n",
    "    while i < num:\n",
    "        yield i\n",
    "        i += 1\n",
    "\n",
    "print(return_fun(10))\n",
    "\n",
    "for i in return_fun(10):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6137ade4",
   "metadata": {},
   "source": [
    "## 混合类型检查\n",
    "联合运算符使用“|”线来代替了就版本中Union[]方法，使得程序更加简洁"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "21d9bd98",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-13T09:28:49.968682Z",
     "start_time": "2023-10-13T09:28:49.963044Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "110\n",
      "110.23\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\\ndef new_func_test(num: int | float) -> int | float:\\n    return num+100\\n\\nprint(new_func_test(10))\\nprint(new_func_test(10.23))\\n'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 旧版本的使用\n",
    "from typing import Union\n",
    "\n",
    "def old_func_test(num: Union[int, float]) -> Union[int, float]:\n",
    "    return num+100\n",
    "\n",
    "print(old_func_test(10))\n",
    "print(old_func_test(10.23))\n",
    "\n",
    "# 新版本中 |\n",
    "\"\"\"\n",
    "def new_func_test(num: int | float) -> int | float:\n",
    "    return num+100\n",
    "\n",
    "print(new_func_test(10))\n",
    "print(new_func_test(10.23))\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20befe3b",
   "metadata": {},
   "source": [
    "## 类型别名更改\n",
    "之前是直接通过不同类型的赋值操作来赋予类型新的名字，在新版本中通过TypeAlias来规定了类型名字的替换。\n",
    "\n",
    "这样操作的优势在于能够让程序开发人员和Python编辑器更加清楚的知道newname是一个变量名还是一个类型的别名，提升程序开发的可靠性。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "64e28d84",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-13T09:33:57.110356Z",
     "start_time": "2023-10-13T09:33:57.104993Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "msg: hellowold\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\"\\nfrom typing import TypeAlias\\n\\nnewstr: TypeAlias = str\\nnewint: TypeAlias = int\\n\\ndef func_test(num: newint, msg: newstr) -> newstr:\\n    return str(num)+msg\\n\\nprint(func_test(100, '类型别名更改'))\\n\""
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 旧版本\n",
    "oldstr = str\n",
    "msg: oldstr = 'hellowold'\n",
    "print('msg:', msg)\n",
    "\n",
    "# 新版本\n",
    "\"\"\"\n",
    "from typing import TypeAlias\n",
    "\n",
    "newstr: TypeAlias = str\n",
    "newint: TypeAlias = int\n",
    "\n",
    "def func_test(num: newint, msg: newstr) -> newstr:\n",
    "    return str(num)+msg\n",
    "\n",
    "print(func_test(100, '类型别名更改'))\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42b46904",
   "metadata": {},
   "source": [
    "## 二进制统计1的个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "189a4a22",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-13T09:34:49.860175Z",
     "start_time": "2023-10-13T09:34:49.856700Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0b100\n",
      "出现1的次数： 1\n"
     ]
    }
   ],
   "source": [
    "num = 4\n",
    "\n",
    "# 旧版本\n",
    "print(bin(num))\n",
    "print('出现1的次数：', bin(num).count('1'))\n",
    "\n",
    "# 新版本 bit_count()\n",
    "# print('出现1的次数：', num.bit_count())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d217a076",
   "metadata": {},
   "source": [
    "## 三个方法新增mapping属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f11a371",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = {'a': 1, 'b': 2, 'c': 3}\n",
    "# print('字典数据：',data)\n",
    "# #items values keys\n",
    "# print('items:',data.items())\n",
    "# print('values:',data.values())\n",
    "# print('keys:',data.keys())\n",
    "\n",
    "# 新特性\n",
    "keys = data.keys()\n",
    "items = data.items()\n",
    "values = data.values()\n",
    "# print('mapping:',keys.mapping)\n",
    "# print('mapping:',items.mapping)\n",
    "# print('mapping:',values.mapping)\n",
    "\n",
    "# 旧版本\n",
    "keys = ['name', 'age', 'sex']\n",
    "values = ['zs', 23, 'man']\n",
    "data_dict = dict(zip(keys, values))\n",
    "print('创建的字典对象：', data_dict)\n",
    "\n",
    "# 新版本\n",
    "data_dict2 = dict(zip(keys, values, strict=True))\n",
    "print('新版添加strict属性：', data_dict2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37844908",
   "metadata": {},
   "source": [
    "## 函数zip()新增strict参数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71e0cd28",
   "metadata": {},
   "source": [
    "对于zip函数加了strict参数，顾名思义strict参数就是要严格的通过参数长度的匹配原则\n",
    "\n",
    "在下面的代码中，keys和values列表的长度并不一致，旧版本的zip函数会根据长度最短的参数创建字典。\n",
    "\n",
    "新版本的zip函数中，当设定strict参数为True，则要求zip的输入数必须要长度一致，否则报错。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "80a3d19b",
   "metadata": {},
   "outputs": [],
   "source": [
    "keys = ['one','two','three','four']\n",
    "values = [1,2,3,4,5] # 旧版本\n",
    "print(dict(zip(keys,values)))\n",
    "\n",
    "# 新版本\n",
    "print(dict(zip(keys,values,strict=True)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06c15b3c",
   "metadata": {},
   "source": [
    "## dataclass\n",
    "**使用类：**\n",
    "\n",
    "为了支持数据修改, 默认值, 比较等功能。更加好一些的方法是：使用自定义类来实现数据类。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c1e33b41",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-13T13:13:41.253258Z",
     "start_time": "2023-10-13T13:13:41.244758Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Player: SXT 18\n"
     ]
    }
   ],
   "source": [
    "class Player:\n",
    "    def __init__(self, name: str, number: int, postion: str, age: int = 18) -> None:\n",
    "        \n",
    "        self.name = name\n",
    "        self.number = number\n",
    "        self.postion = postion\n",
    "        self.age = age\n",
    "        \n",
    "    def __repr__(self) -> str:\n",
    "        return f'Player: {self.name} {self.number}'\n",
    "    \n",
    "    def __eq__(self, __o: object) -> bool:\n",
    "        return self.age == __o.age\n",
    "    \n",
    "    def __gt__(self,__o: object) -> bool:\n",
    "        return self.age > __o.age\n",
    "    \n",
    "p1 = Player('SXT',18,'PG',26)\n",
    "print(p1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce27f11e",
   "metadata": {},
   "source": [
    "**缺点：**\n",
    "- __init__ 方法中重复代码 (示例中每个属性都需要写3遍)\n",
    "- 需要自己实现 __repr__ 方法, 和比较方法 __eq__ , __gt__ 等"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f46bbcf",
   "metadata": {},
   "source": [
    "**使用dataclass：**\n",
    "\n",
    "dataclass 可以认为是提供了一个简写 __init__ 方法的语法，类型注释是必填项 (不限制数据类型时, 添加typing.Any为类型注释), 默认值的传递方式和 __init__ 方法的参数格式一致。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40bcf054",
   "metadata": {},
   "source": [
    "**dataclass装饰器上的参数**\n",
    "\n",
    "dataclass 装饰器将根据类属性生成数据类和数据类需要的方法。\n",
    "![](images/23.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "953163fd",
   "metadata": {},
   "source": [
    "![](images/24.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0b75e45",
   "metadata": {},
   "source": [
    "**dataclass成员变量额外设置**\n",
    "可以通过 dataclass.filed 方法设置变量的额外功能\n",
    "- defualt:设置默认值\n",
    "    - 值为具体的值\n",
    "- default_factory:设置默认值\n",
    "    - 值为类型名，程序会根据类型名创建一个空的对象，若使用defualt设置需要手动创建空对象\n",
    "- repr设置生成的 __repr__ 方法中是否加入此属性, 默认是True"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98935886",
   "metadata": {},
   "source": [
    "**dataclass建立类变量**\n",
    "\n",
    "在类中建立变量，默认是成员变量，若需要设置类变量，需要设置类型为： ClassVar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "411c2ae2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-13T13:15:52.540813Z",
     "start_time": "2023-10-13T13:15:52.531451Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Player(name='zs', postion='北京', age=24, msg='')\n",
      "类属性： 中国\n"
     ]
    }
   ],
   "source": [
    "from dataclasses import dataclass, field\n",
    "from typing import ClassVar\n",
    "\n",
    "\n",
    "@dataclass\n",
    "class Player:\n",
    "    name: str\n",
    "    postion: str\n",
    "    age: int\n",
    "        \n",
    "    # 设置默认值\n",
    "    sex: str = field(default='man', repr=False)\n",
    "    \n",
    "    msg: str = field(default_factory=str)\n",
    "\n",
    "    # 类变量\n",
    "    country: ClassVar[str]\n",
    "\n",
    "\n",
    "# 创建实例对象\n",
    "p = Player('zs', '北京', 24)\n",
    "print(p)\n",
    "\n",
    "Player.country = '中国'\n",
    "print('类属性：', Player.country)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89e41519",
   "metadata": {},
   "source": [
    "## 字典合并\n",
    "字典添加两个新的运算符：[|]和[|=]。[|]运算符用于合并字典。[|=]用于更新字典。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d4133d5f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-13T13:21:58.258181Z",
     "start_time": "2023-10-13T13:21:58.249472Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 1, 'b': 2}\n",
      "{'a': 1, 'b': 2}\n",
      "{'a': 1, 'b': 2}\n"
     ]
    }
   ],
   "source": [
    "dict1 = {'a': 1}\n",
    "dict2 = {'b': 2}\n",
    "\n",
    "# 旧版本\n",
    "dict1.update(dict2)\n",
    "print(dict1)\n",
    "\n",
    "# 新版本 |\n",
    "result = dict1 | dict2\n",
    "print(result)\n",
    "\n",
    "# 更新字典\n",
    "dict1 |= dict2  # 等价于 dict1 = dict1 | dict2\n",
    "print(dict1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a8c2d9fa",
   "metadata": {},
   "source": [
    "## match语法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb858f53",
   "metadata": {},
   "source": [
    "match...case结构化模式匹配，可以匹配字典、类以及其他更复杂的结构。\n",
    "\n",
    "match...case的匹配模式匹配于Java或C++中的switch的使用很相似"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "9eb46420",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-13T13:24:11.934881Z",
     "start_time": "2023-10-13T13:24:11.928955Z"
    },
    "collapsed": true
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (269965408.py, line 20)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  Input \u001b[0;32mIn [16]\u001b[0;36m\u001b[0m\n\u001b[0;31m    match person:\u001b[0m\n\u001b[0m          ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "status = 400\n",
    "\n",
    "match status:\n",
    "    case 200:\n",
    "        print('访问成功')\n",
    "    case 404:\n",
    "        print('访问的资源不存在')\n",
    "    case 500:\n",
    "        print('服务器错误')\n",
    "    case _:\n",
    "        print('以上都没有匹配成功')\n",
    "\n",
    "p1 = ('zs', 23, 'man')\n",
    "p2 = ('lili', 24, 'woman')\n",
    "p3 = ('xiaoqiang', 25, 'man')\n",
    "p4 = ('xiaoqiang', 25, 'male')\n",
    "\n",
    "\n",
    "def func_test(person):\n",
    "    match person:\n",
    "        case(name, _, 'man'):\n",
    "            print(f'{name} is man')\n",
    "        case(name, _, 'woman'):\n",
    "            print(f'{name} is woman')\n",
    "        case(name, age, sex):\n",
    "            print(f'{name=},{age=},{sex=}')\n",
    "\n",
    "\n",
    "# 调用函数\n",
    "func_test(p1)\n",
    "func_test(p2)\n",
    "func_test(p3)\n",
    "func_test(p4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "86926447",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "248.1875px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
